package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.gt3.entity.ZxhjBlxkEntity;
import cn.com.css.taxInspection.gt3.mapper.ZxhjBlxkMapper;
import cn.com.css.taxInspection.gt3.service.ZxhjBlxkService;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service("ZxhjBlxkService")
public class ZxhjBlxkServiceImpl extends ServiceImpl<ZxhjBlxkMapper, ZxhjBlxkEntity> implements ZxhjBlxkService {

    @Resource
    ZxhjBlxkMapper zxhjBlxkMapper;

    @Override
    public List<ZxhjBlxkEntity> queryBlxkTable(ZxhjBlxkEntity zxhjBlxkEntity) {
        List<ZxhjBlxkEntity> zxhjBlxkEntityList = new ArrayList<>();

        // 判断是否传入了特定月份
        String specifiedMonth = TsBqUtils.isNotNull(zxhjBlxkEntity.getYf()) ? zxhjBlxkEntity.getYf() : DateUtils.getCurrentMonth();

        // 1. 确定查询基准月份
        YearMonth baseMonth;
        if (specifiedMonth != null && !specifiedMonth.isEmpty()) {
            // 使用传入的月份作为基准
            baseMonth = YearMonth.parse(specifiedMonth, DateTimeFormatter.ofPattern("yyyyMM"));
        } else {
            // 使用当前月份作为基准
            baseMonth = YearMonth.now();
        }
        String baseMonthStr = baseMonth.format(DateTimeFormatter.ofPattern("yyyyMM"));

        String swjgmc = zxhjBlxkEntity.getSwjgmc();

        if (swjgmc == null ||swjgmc.equals("")) {
            extracted("沈阳市税务稽查局",baseMonthStr,zxhjBlxkEntityList);
            extracted("沈阳市税务局第一稽查局",baseMonthStr,zxhjBlxkEntityList);
            extracted("沈阳市税务局第二稽查局",baseMonthStr,zxhjBlxkEntityList);
            extracted("沈阳市税务局第三稽查局",baseMonthStr,zxhjBlxkEntityList);

        }else {
            extracted(swjgmc,baseMonthStr,zxhjBlxkEntityList);
        }


        return zxhjBlxkEntityList;
    }


    private void extracted(String swjgmc, String baseMonthStr, List<ZxhjBlxkEntity> zxhjBlxkEntityList) {
        List<ZxhjBlxkEntity> zxhjBlxkEntities = zxhjBlxkMapper.findBlxkTable(baseMonthStr,swjgmc);
        if (zxhjBlxkEntities == null || zxhjBlxkEntities.isEmpty()) {
            ZxhjBlxkEntity zxhjBlxkEntity = new ZxhjBlxkEntity();
            zxhjBlxkEntity.setSwjgmc(swjgmc);
            zxhjBlxkEntityList.add(zxhjBlxkEntity);
        }else {
            zxhjBlxkEntityList.addAll(zxhjBlxkEntities);
        }
    }



}